package com.gitee.mars.tourserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.tourserver.constant.Constant;
import com.gitee.mars.tourserver.entity.SysUser;
import com.gitee.mars.tourserver.exception.WorkException;
import com.gitee.mars.tourserver.mapper.SysUserMapper;
import com.gitee.mars.tourserver.request.SysUserLoginRequest;
import com.gitee.mars.tourserver.request.SysUserRegisterRequest;
import com.gitee.mars.tourserver.request.SysUserRequest;
import com.gitee.mars.tourserver.response.SysUserResponse;
import com.gitee.mars.tourserver.response.base.PageInfo;
import com.gitee.mars.tourserver.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.Objects;


/**
 * SysUser 实现层
 *
 * @Author wq
 * @Date 2021-03-15
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {


    @Override
    public SysUserResponse login(SysUserLoginRequest request) {
        //查询该用户
        SysUser sysUser = baseMapper.selectNickname(request.getNickname());
        if (Objects.isNull(sysUser)) {
            throw new WorkException("该用户还未注册,请注册");
        }
        if (!sysUser.getPassword().equals(request.getPassword())) {
            throw new WorkException("请输入正确的密码");
        }
        SysUserResponse sysUserResponse = new SysUserResponse();
        sysUserResponse.setAvatar(sysUser.getAvatar());
        sysUserResponse.setUserId(sysUser.getId());
        sysUserResponse.setNickname(sysUser.getNickname());
        return sysUserResponse;
    }

    @Override
    public PageInfo<SysUser> listUser(SysUserRequest request) {
        IPage<SysUser> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(request.getUserId())) {
            wrapper.eq(SysUser::getId, request.getUserId());
        }
        if (StringUtils.isNotEmpty(request.getNickname())) {
            wrapper.like(SysUser::getNickname, request.getNickname());
        }
        wrapper.orderByDesc(SysUser::getCreateTime);
        IPage<SysUser> sysUserIPage = baseMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(sysUserIPage.getRecords())) {
            return new PageInfo<>(page.getTotal(), sysUserIPage.getRecords());
        }
        return null;
    }

    @Override
    public void resetPassword(SysUserRequest request) {
        SysUser sysUser = baseMapper.selectOne(new LambdaQueryWrapper<SysUser>().
                eq(SysUser::getNickname, request.getNickname()).eq(SysUser::getMobile, request.getMobile()).last("limit 1"));
        if (Objects.isNull(sysUser)) {
            throw new WorkException("请输入正确的账号昵称");
        }
        sysUser.setPassword("12345");
        sysUser.setUpdateTime(new Date());
        updateById(sysUser);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public SysUserResponse register(SysUserRegisterRequest request) {

        if (StringUtils.isEmpty(request.getNickname())) {
            throw new WorkException("请输入昵称");
        }
        if (StringUtils.isEmpty(request.getPassword())) {
            throw new WorkException("请输入密码");
        }
        if (StringUtils.isEmpty(request.getMobile())) {
            throw new WorkException("请输入手机号");
        }
        SysUser selectOne = baseMapper.selectOne(new LambdaQueryWrapper<SysUser>().
                eq(SysUser::getMobile, request.getMobile()));
        if (Objects.nonNull(selectOne)) {
            throw new WorkException("该账号已注册");
        }
        SysUser sysUser = new SysUser();
        sysUser.setAvatar(Constant.DEFAULT_AVATAR);
        sysUser.setNickname(request.getNickname());
        sysUser.setMobile(request.getMobile());
        sysUser.setEmail("850994581@qq.com");
        sysUser.setDeptId(4);
        sysUser.setRoleId(1);
        sysUser.setUpdateTime(new Date());
        sysUser.setCreateTime(new Date());
        sysUser.setPassword(request.getPassword());
        sysUser.setDeleted(Constant.ZERO);
        save(sysUser);

        SysUserResponse sysUserResponse = new SysUserResponse();
        sysUserResponse.setAvatar(sysUser.getAvatar());
        sysUserResponse.setUserId(sysUser.getId());
        sysUserResponse.setNickname(sysUser.getNickname());
        return sysUserResponse;
    }
}